package com.ruoyi.web.controller.interview;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.interview.domain.entity.ReportDetailEntity;
import com.ruoyi.interview.domain.entity.ReportDimensionDetail;
import com.ruoyi.interview.domain.entity.ReportResponseEntity;
import com.ruoyi.interview.service.IReportGenerateService;
import com.ruoyi.web.util.ChatUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interview.domain.InterviewReport;
import com.ruoyi.interview.service.IInterviewReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 面试报告Controller
 * 
 * @author xwjdxx
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/interview/report")
public class InterviewReportController extends BaseController
{
    @Autowired
    private IInterviewReportService interviewReportService;

    /**
     * 查询面试报告列表
     */
    @PreAuthorize("@ss.hasPermi('interview:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(InterviewReport interviewReport)
    {
        startPage();
        List<InterviewReport> list = interviewReportService.selectInterviewReportList(interviewReport);
        return getDataTable(list);
    }

    /**
     * 导出面试报告列表
     */
    @PreAuthorize("@ss.hasPermi('interview:report:export')")
    @Log(title = "面试报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InterviewReport interviewReport)
    {
        List<InterviewReport> list = interviewReportService.selectInterviewReportList(interviewReport);
        ExcelUtil<InterviewReport> util = new ExcelUtil<InterviewReport>(InterviewReport.class);
        util.exportExcel(response, list, "面试报告数据");
    }

    /**
     * 获取面试报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('interview:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(interviewReportService.selectInterviewReportById(id));
    }

    /**
     * 新增面试报告
     */
    @PreAuthorize("@ss.hasPermi('interview:report:add')")
    @Log(title = "面试报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InterviewReport interviewReport)
    {
        return toAjax(interviewReportService.insertInterviewReport(interviewReport));
    }

    /**
     * 修改面试报告
     */
    @PreAuthorize("@ss.hasPermi('interview:report:edit')")
    @Log(title = "面试报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InterviewReport interviewReport)
    {
        return toAjax(interviewReportService.updateInterviewReport(interviewReport));
    }

    /**
     * 删除面试报告
     */
    @PreAuthorize("@ss.hasPermi('interview:report:remove')")
    @Log(title = "面试报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(interviewReportService.deleteInterviewReportByIds(ids));
    }

    @GetMapping("/genReport")
    public AjaxResult genReport(Long recordId ){

        // 每隔1s判断当前录音是否已经生成完毕
        while(true){
            int result = interviewReportService.getResult(recordId);
            System.out.println("result=================================" + result);
            if(result > 0){
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        // 生成提示词
        String prompt = interviewReportService.genPrompt(recordId);
        // 根据提示词调用AI接口，获得评价
        String report = ChatUtil.getService(IReportGenerateService.class).generateReport(prompt);
        ReportResponseEntity reportResponseEntity = JSONObject.parseObject(report, ReportResponseEntity.class);
        // 保存到数据库
        reportResponseEntity.setRecordId(recordId);
        interviewReportService.saveReport(reportResponseEntity);
        return AjaxResult.success();
    }

    /**
     * 列表当前登陆人的面试报告
     * @return
     */
    @GetMapping("/listReport")
    public AjaxResult listReport(){
        List<InterviewReport> reports = interviewReportService.selectList();
        return AjaxResult.success(reports);
    }

    /**
     * 查询报告详情
     * @param reportId
     * @return
     */
    @GetMapping("/reportDetail")
    public AjaxResult reportDetail(Long reportId){
        ReportDetailEntity detail = interviewReportService.reportDetail(reportId);
        // 打印 detail 对象信息
        System.out.println("查询到的报告详情信息: " + detail);
        return AjaxResult.success(detail);
    }
}
